package com.erp.common.web;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.erp.app.common.ProjectConstants;
import com.erp.app.common.search.beans.SearchDelegate;
import com.erp.app.common.search.beans.SearchVO;
import com.erp.app.util.web.WebUtil;
import com.erp.framework.SimpleConstants;
import com.erp.framework.controller.action.SimpleUserStyleAction;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.logger.Logger;


public class SelectRemarkTextAction extends SimpleUserStyleAction
{

    public static final String STATUS_SP_SET = SimpleConstants.STATUS_SPSET;

	protected String getModuleName(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response){
        return "";
	}
	
    public ActionForward otherExecute(ActionMapping mapping,
								 ActionForm formIn,
								 HttpServletRequest request,
								 HttpServletResponse response)
		throws ServletException
    {
    	
		HttpSession session = request.getSession(false);
		ActionErrors errors = new ActionErrors();
		MessageResources messages = getResources();

		SelectRemarkTextForm form = (SelectRemarkTextForm) formIn;
		String action = form.getPageEvent();
		action = (action == null) ? "" : action.trim();
		ActionForward actionForward = new ActionForward(mapping.getInput());

		boolean handled = false;

		HashMap params = (HashMap) session.getAttribute(ProjectConstants.SESSION_SELECT_REMARK_TEXT_PARAMETERS);
		session.removeAttribute(ProjectConstants.SESSION_SELECT_REMARK_TEXT_PARAMETERS);

		if (params != null) {
			form.setParams(params);
            if (CommonUtil.isEmpty(form.getSelectedRemarkMaster()) && !CommonUtil.isEmpty(params.get(ProjectConstants.SESSION_EDIT_SELECTED_REMARK_MASTER))) form.setSelectedRemarkMaster((String) params.get(ProjectConstants.SESSION_EDIT_SELECTED_REMARK_MASTER));
			form.setRemarkColumnName((String) params.get(ProjectConstants.SESSION_EDIT_COLUMN_NAME));
			form.setRemarkText((String) params.get(ProjectConstants.SESSION_EDIT_VALUE));
			form.setAppend(ProjectConstants.GENERIC_YES);
			setHandled(request);
		}

		if (!isHandled(request) && action.equalsIgnoreCase("changeMaster")) {
			//form.setRemarkText("");
			changeRemarkDtl(form, true, session);
			setHandled(request);
		}

		if (!isHandled(request) && action.equalsIgnoreCase("changeDtl")) {
			changeRemarkDtl(form, false, session);
			setHandled(request);
		}

		if (!isHandled(request) && action.equalsIgnoreCase("ok")) {
			HashMap params2 = form.getParams();
			String sessionValueName = (String) params2.get(ProjectConstants.SESSION_SESSION_VALUE);
			params2.put(ProjectConstants.SESSION_EDIT_VALUE, form.getRemarkText());
			session.setAttribute(sessionValueName, STATUS_SP_SET);
			session.setAttribute(ProjectConstants.SESSION_SELECT_REMARK_TEXT_PARAMETERS, params2);
			setHandled(request);
		}
		if (!isHandled(request) && action.equalsIgnoreCase("erase")) {
			form.setRemarkText("");
			setHandled(request);
		}


		saveErrors(request, errors);
		return actionForward;
    }

	private void changeRemarkDtl(SelectRemarkTextForm form, boolean getDefault, HttpSession session)
	{
		SearchDelegate dg = new SearchDelegate();

		String sqlStr;
		HashMap params2 = form.getParams();
		String sessionValueName = (String) params2.get(ProjectConstants.SESSION_SESSION_VALUE);
		String where = "";

		where = "COMPANY_CODE = '" + CommonUtil.sqlEncode(WebUtil.getAppropriateCompanyCode(session, ProjectConstants.SP_SHARE_REMARK)) + "'";
		where += " AND REMARK_MASTER_CODE='" + CommonUtil.sqlEncode(form.getSelectedRemarkMaster()).toUpperCase() + "'";

		sqlStr = "";
		sqlStr += "SELECT REMARK COLUMN1, REMARK_DTL_CODE COLUMN2";

		for (int i=2; i<50; i++) {
			sqlStr += ", '' COLUMN" + (i+1);
		}

		sqlStr += " FROM T_REMARK_DTL";
		sqlStr += " WHERE " + where;
		sqlStr += " AND ";

		if(getDefault){
			sqlStr += " IS_DEFAULT='"+ProjectConstants.GENERIC_YES+"'";
		} else {
			sqlStr += " REMARK_DTL_CODE ='" + CommonUtil.sqlEncode(form.getSelectedRemarkDtl().toUpperCase()) + "'";
		}

		try{
			ArrayList srchRst = dg.searchSearch(sqlStr.toUpperCase());
			if((!srchRst.isEmpty())){
				SearchVO dtlVO = (SearchVO) srchRst.get(0);
				
				
				if (CommonUtil.nullToString(form.getAppend()).equals(ProjectConstants.GENERIC_YES)) {
					StringBuffer sb = new StringBuffer();
					sb.append(CommonUtil.nullToString(form.getRemarkText())+"\n");
					sb.append(CommonUtil.nullToString(dtlVO.getColumn1()));					
					form.setRemarkText(sb.toString());
				} else {				    
					form.setRemarkText(CommonUtil.nullToString(dtlVO.getColumn1()));
				}
				form.setSelectedRemarkDtl(CommonUtil.nullToString(dtlVO.getColumn2()));
			}

			srchRst.clear();
		} catch (Exception e) {
		    Logger.logExceptions(this, e);
		}
	}
}
